﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_ProductStore_ProductStore : System.Web.UI.Page
{
    protected string pDel_mk = "", pUpd_mk = "";
    protected int iPageIndex = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        if (!IsPostBack)
        {

            clsGetMenuAuth clsAuth = new clsGetMenuAuth(Session["AdminUserID"].ToString(), Session["AutID"].ToString(), Session["IsAdmin"].ToString(), Request.QueryString["CodeID"]);
            if (clsAuth.IsDeleteAuth() == true)
                pDel_mk = "Y";
            if (clsAuth.IsUpdateAuth() == true)
                pUpd_mk = "Y";
            txtTenSP.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearch.ClientID + "').click();return false;}} else {return true}; ");

            BindStoreList();
            BindTopicList();
            BindCatalogueByTopicID();

            if (Request.QueryString["PageIndex"] != "" && Request.QueryString["PageIndex"] != null)
            {
                iPageIndex = int.Parse(Request.QueryString["PageIndex"]);
                grv_Master.PageIndex = iPageIndex;
            }
            BinData();
        }


    }

    private void CountStoreProduct()
    {
        clsWebShop cls = new clsWebShop();
        //   lblCountStore.Text = ddlStore.SelectedItem.Text + " có: " + cls.Get_CountStorerByCondition(ddlStore.SelectedValue) +" sản phẩm";
        Session["dtCountStore"] = cls.Get_QuantityStoreGroup("", ddlStore.SelectedValue).Tables[0];
    }
    protected string fnURLImage(string Quantity)
    {
        try
        {
            if (int.Parse(Quantity) > 0)
                return "../../Images/Icon/substractStore.png";
            else
                return "../../Images/Icon/store.png";
        }
        catch { return "../../Images/Icon/store.png"; }
    }

    private void BindStoreList()
    {
        clsWebShop cls = new clsWebShop();
        ListItem item = new ListItem();
        ddlStore.DataSource = cls.Get_StoreByKindNoNoneProc().Tables[0];
        ddlStore.DataTextField = "StName";
        ddlStore.DataValueField = "StID";
        ddlStore.DataBind();
        item.Text = "Tất cả";
        item.Value = "0";
        ddlStore.Items.Insert(0, item);
        ddlStore.SelectedItem.Selected = false;

        if (Request.QueryString["StID"] != "" && Request.QueryString["StID"] != null)
            ddlStore.Items.FindByValue(Request.QueryString["StID"]).Selected = true;
    }

    private void BindTopicList()
    {
        clsWebShop cls = new clsWebShop();
        ListItem item = new ListItem();
        ddlChuDe.DataSource = cls.Get_TopicNoneProc().Tables[0];
        ddlChuDe.DataTextField = "TopicName";
        ddlChuDe.DataValueField = "TopicID";
        ddlChuDe.DataBind();
        item.Text = "-- Chọn chủ đề --";
        item.Value = "0";
        ddlChuDe.Items.Insert(0, item);
        ddlChuDe.SelectedItem.Selected = false;

        if (Request.QueryString["TopicID"] != "" && Request.QueryString["TopicID"] != null)
            ddlChuDe.Items.FindByValue(Request.QueryString["TopicID"]).Selected = true;
    }

    private void BindCatalogueByTopicID()
    {
        clsWebShop cls = new clsWebShop();

        ListItem item = new ListItem();
        DataTable dt = null;
        dt = cls.Get_CatalogueByTopicIDNoneProc(ddlChuDe.SelectedValue).Tables[0];
        ddlTheLoai.DataSource = dt;
        ddlTheLoai.DataTextField = "Catname";
        ddlTheLoai.DataValueField = "CatID";
        ddlTheLoai.DataBind();
        item.Text = "-- Chọn thể loại--";
        item.Value = "0";
        ddlTheLoai.Items.Insert(0, item);
        try
        {
            if (Request.QueryString["CatID"] != "" && Request.QueryString["CatID"] != null)
                ddlTheLoai.Items.FindByValue(Request.QueryString["CatID"]).Selected = true;
        }
        catch (Exception ex) { }
    }
    private void BinData()
    {
        PccMsg msg = new PccMsg();

        msg.CreateFirstNode("PdName", txtTenSP.Text.Trim());
        msg.CreateFirstNode("CatID", ddlTheLoai.SelectedValue);
        msg.CreateFirstNode("TopicID", ddlChuDe.SelectedValue);
        msg.CreateFirstNode("KindNo", "P");
        msg.CreateFirstNode("StID", "0");
        clsWebShop cls = new clsWebShop();

        //Lay so luong trong kho
        //  lblCountStore.Text = ddlStore.SelectedItem.Text + " có: " + cls.Get_CountStorerByCondition(ddlStore.SelectedValue) + " sản phẩm";
        Session["dtCountStore"] = cls.Get_QuantityStoreGroup("", ddlStore.SelectedValue).Tables[0];

        //Bind du lieu
        DataSet ds = null;
        ds = cls.Get_ProductByCondition(msg.GetXmlStr);
        grv_Master.DataSource = ds;
        grv_Master.DataBind();

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BinData();
    }
    protected void ddlChuDe_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCatalogueByTopicID();
    }


    protected void grv_Master_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Master.PageIndex = e.NewPageIndex;
        iPageIndex = e.NewPageIndex;
        BinData();
    }
    protected void ddlStore_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinData();
        CountStoreProduct();
    }
    protected void grv_Master_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.className='cursorOverGrid'");
            if (e.Row.RowIndex % 2 == 0)
                e.Row.Attributes.Add("onmouseout", "this.className='gvRowStyle'");
            else
                e.Row.Attributes.Add("onmouseout", "this.className='gvAlternatingRowStyle'");

            e.Row.Cells[5].Text = "<a onclick=\"window.open(this.href,'viewwin','menu=0,scrollbars=1,status=1,location=0,resizable=1')\" href='ProductStoreDetail.aspx?StID=" + ddlStore.SelectedValue + "&PDID=" + DataBinder.Eval(e.Row.DataItem, "PDID").ToString() + "' target='viewwin'><img src='../../Images/Icon/ViewStore.png' border='0' width='25px' height='25px' alt='Xem chi tiết' /> </a>";

            // e.Row.Cells[5].Text = "<a  href='InputProductStore.aspx?StID="+ddlStore.SelectedValue+"&PDID="+  DataBinder.Eval(e.Row.DataItem, "PDID").ToString()  +"&Type=1&CodeID="+Request.QueryString["CodeID"]+"' ><img src='../../Images/Icon/updateShote.png' border='0'  width='25px' height='25px' alt='Cập nhật kho'/> </a>";
            e.Row.Cells[6].Text = "<img alt='Cập nhật kho' border='0' height='25px'  style='cursor:pointer' onclick='fnOpenOrderDetail(\"" + DataBinder.Eval(e.Row.DataItem, "PDID").ToString() + "\"," + Request.QueryString["CodeID"] + "," + ddlStore.SelectedValue + ",this,1)'    src=../../Images/Icon/updateShote.png width=25px /> ";


            string sQ_Store = "0";
            try
            {
                DataTable dt = (DataTable)Session["dtCountStore"];
                sQ_Store = dt.Select(" PDID='" + DataBinder.Eval(e.Row.DataItem, "PDID").ToString() + "'")[0]["Quantity_Store"].ToString();
            }
            catch
            {
                sQ_Store = "0";
            }

            if (int.Parse(sQ_Store) > 0)
            {
                e.Row.Cells[7].Text = "<img alt='Cập nhật kho' border='0' height='25px'  style='cursor:pointer' onclick='fnOpenOrderDetail(\"" + DataBinder.Eval(e.Row.DataItem, "PDID").ToString() + "\"," + Request.QueryString["CodeID"] + "," + ddlStore.SelectedValue + ",this,0)'    src=../../Images/Icon/substractStore.png width=25px /> ";
                e.Row.Cells[8].Text = "<img  border='0' height='25px'   src='../../Images/Icon/store.png' width='25px' style='cursor:pointer'   onclick='fnOpenOrderDetail(\"" + DataBinder.Eval(e.Row.DataItem, "PDID").ToString() + "\"," + Request.QueryString["CodeID"] + "," + ddlStore.SelectedValue + ",this,2)' >";

            }
            else
            {
                e.Row.Cells[3].BackColor = System.Drawing.Color.Orange;
            }
            e.Row.Cells[3].Text = sQ_Store;

            //Truong hop xoa
            if (Session["IsAdmin"].ToString() != "S" && pDel_mk != "Y")
            {
                e.Row.Cells[7].Text = "";
                e.Row.Cells[7].Attributes.Add("onclick", "javascript:return false;");
            }
            //Truong hop Update
            if (Session["IsAdmin"].ToString() != "S" && pUpd_mk != "Y")
            {
                e.Row.Cells[6].Text = "";
                e.Row.Cells[6].Attributes.Add("onclick", "javascript:return false;");
            }
            clsCommon clsCom=new clsCommon();
            Label lblPrice = (Label)e.Row.FindControl("lblPrice");
            lblPrice.Text = clsCom.FormatMoney(lblPrice.Text);
        }
    }
}
